<template>
  <a-config-provider :locale="zhCN">
    <router-view />
  </a-config-provider>
</template>

<script setup>
import zhCN from 'ant-design-vue/es/locale/zh_CN'
import { onMounted, onUnmounted } from 'vue'
import { useAuthStore } from './stores/auth'
import { useRouter } from 'vue-router'

const authStore = useAuthStore()
const router = useRouter()

let tokenCheckInterval = null

const checkTokenValidity = async () => {
  if (authStore.isAuthenticated) {
    try {
      const isValid = await authStore.checkAuth()
      if (!isValid) {
        // Token失效，跳转到登录页
        if (router.currentRoute.value.path !== '/login') {
          router.push('/login')
        }
      }
    } catch (error) {
      console.error('Token检查失败:', error)
    }
  }
}

onMounted(async () => {
  // 应用启动时检查认证状态
  await authStore.checkAuth()
  
  // 设置定时检查Token有效性（每5分钟检查一次）
  tokenCheckInterval = setInterval(checkTokenValidity, 5 * 60 * 1000)
})

onUnmounted(() => {
  // 清理定时器
  if (tokenCheckInterval) {
    clearInterval(tokenCheckInterval)
  }
})
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

#app {
  height: 100%;
}
</style>